from django.shortcuts import render
from rest_framework.generics import GenericAPIView
# Create your views here.
from rest_framework.response import Response

from api.models import Book, User
from api.serializers import UserSerializer, BookModelSerializerV3
from rest_framework import mixins, status
from rest_framework import generics

from rest_framework import viewsets


class BookGenericAPIView(GenericAPIView,
                         mixins.ListModelMixin,
                         mixins.RetrieveModelMixin,
                         mixins.DestroyModelMixin,
                         mixins.UpdateModelMixin):
    queryset = Book.objects.filter()
    serializer_class = BookModelSerializerV3

    lookup_field = "id"

    def get(self, request, *args, **kwargs):
        if "id" in kwargs:
            # 查询单个  mixins.RetrieveModelMixin
            return self.retrieve(request, *args, **kwargs)
        return self.list(request, *args, **kwargs)

    def delete(self, request, *args, **kwargs):
        # mixins.DestroyModelMixin
        return self.destroy(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        return self.partial_update(request, *args, **kwargs)

    def put(self, requset, *args, **kwargs):
        return self.partial_update(requset, *args, **kwargs)

    # def get1(self, request, *args, **kwargs):
    #     # 获取book模型中所有的数据
    #     # book_list = Book.objects.all()
    #     book_list = self.get_queryset()
    #
    #     # 获取序列化器
    #     # serializer_data = BookModelSerializerV3(book_list, many=True).data
    #     serializer = self.get_serializer(book_list, many=True).data
    #     return Response({
    #         "status": 200,
    #         "message": "查询所有图书成功",
    #         "results": serializer,
    #     })
    #
    # def get2(self, request, *args, **kwargs):
    #     book_obj = self.get_object()
    #     serializer = self.get_serializer(book_obj, many=True).data
    #
    #     return Response({
    #         "status": 200,
    #         "message": "查询所有数据成功",
    #         "results": serializer,
    #     })


class BookGenericAPIViewV3(generics.ListAPIView):
    queryset = Book.objects.all()
    serializer_class = BookModelSerializerV3
    lookup_field = "id"


class UserViewSetView(viewsets.GenericViewSet, mixins.ListModelMixin):
    queryset = User.objects.all()
    serializer_class = UserSerializer

    # 登陆
    def user_login(self, request, *args, **kwargs):
        request_data = request.data
        print(request_data, 'user_login')
        print(request_data['name'], 'user_login')
        name = request_data['name']
        password = request_data['password']
        result = User.objects.filter(name=name, password=password)
        print(result,'result')
        if result:
            return Response({
                'status': status.HTTP_200_OK,
                'message': '登陆成功',
            })
        return Response({
            'status': status.HTTP_400_BAD_REQUEST,
            'message': '登陆失败',
        })

    # 注册
    def user_register(self, request, *args, **kwargs):
        request_data = request.data
        name = request_data['name']
        password = request_data['password']
        print(request_data, 'user_register')
        result = User.objects.filter(name=name)
        if result:
            return Response({
                'status': status.HTTP_400_BAD_REQUEST,
                'message': "用户名已存在",
            })
        # 对用户名反序列化
        serializer = UserSerializer(data=request_data)
        serializer.is_valid(raise_exception=True)
        obj = serializer.save()
        return Response({
            'status': status.HTTP_200_OK,
            'message': "注册成功",
            'results': UserSerializer(obj).data
        })

    def get_user_count(self, request, *args, **kwargs):
        # 完成获取用户数量的逻辑
        print("用户数量查询成功")
        return self.list(request, *args, **kwargs)
